package com.itheima.package4_tool;

import java.util.concurrent.CountDownLatch;

public class MyCountDownLatchDemo {
    public static void main(String[] args) {
        //创建CountDownLatch对象
        CountDownLatch cd = new CountDownLatch(3);
        //开三条线程先执行--执行完毕，计数器-1
        //第一条线程----------------------------------------------------------------------------
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName() + "在吃第" + i + "个饺子");
            }
            cd.countDown();
        }, "刘备").start();
        //第二条线程----------------------------------------------------------------------------
        new Thread(() -> {
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName() + "在吃第" + i + "个饺子");
            }
            cd.countDown();
        }, "关羽").start();
        //第三条线程-------------------------------------------------------------------
        new Thread(() -> {
            for (int i = 1; i <= 20; i++) {
                System.out.println(Thread.currentThread().getName() + "在吃第" + i + "个饺子");
            }
            cd.countDown();
        }, "张飞").start();

        //开一条线程后执行--先等待，当计数器为0时，自动唤醒
        new Thread(() -> {
            try {
                cd.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "在收拾碗筷");
            cd.countDown();
        }, "妈妈").start();
    }
}
